﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace CleaningLady
{

    public partial class CleaningLadyForm : Form
    {
        delegate void SetTextCallback(string text);
        Dictionary<string, string[]> FileTypes = new Dictionary<string, string[]>();

        public CleaningLadyForm()
        {
            InitializeComponent();

            FileTypes.Add("Documents", new string[] { ".doc", ".docx", ".odf", ".odt", ".pdf", ".rtf", ".txt", ".xls", ".xlsm", ".xlsx" });
            FileTypes.Add("Images", new string[] { ".bmp", ".gif", ".jpg", ".jpeg", ".png" });
            FileTypes.Add("Movies", new string[] { ".avi", ".mpg", ".mpeg" });
        }

        private void btnSelectPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            
            txtPath.Text = folderBrowserDialog1.SelectedPath;
            
            if (Directory.Exists(folderBrowserDialog1.SelectedPath))
                btnCleanUp.Enabled = true;
        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

        private void btnCleanUp_Click(object sender, EventArgs e)
        {
            var directory = txtPath.Text;

            var thread = new Thread(() => SortFiles(directory));
            thread.Start();
        }

        private void SetText(string text)
        {
            if (this.txtOutput.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                txtOutput.Text += text;
            }
        }

        private void SortFiles(string path)
        {
            var files = Directory.GetFiles(path);

            if (files.Length == 0)
                SetText("No cleaning was necessary.\r\n");
            else
            {
                for (int i = 0; i < files.Length; i++)
                {
                    var info = new FileInfo(files[i]);

                    var newDirectory = this.FileTypes.Where(kvp => kvp.Value.Where(value => value == info.Extension.ToLower()).Count() == 1).SingleOrDefault().Key;

                    if (!String.IsNullOrEmpty(newDirectory))
                    {
                        try
                        {
                            if (!Directory.Exists(path + "\\" + newDirectory))
                            {
                                Directory.CreateDirectory(path + "\\" + newDirectory);
                                SetText("Directory " + path + "\\" + newDirectory + " not found: Created\r\n");
                            }

                            Directory.Move(info.FullName, path + "\\" + newDirectory + "\\" + info.Name);
                            SetText("Moved file: " + info.FullName + " to " + path + "\\" + newDirectory + "\r\n");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            break;
                        }
                    }

                    if (i == (files.Length - 1))
                    {
                        SetText("\r\nThe CleaningLady is done!\r\n");
                        MessageBox.Show("The CleaningLady is done!", "Mission accomplished", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }   
                }
            }
        }

        private void txtOutput_TextChanged(object sender, EventArgs e)
        {
            txtOutput.SelectionStart = txtOutput.Text.Length;
            txtOutput.ScrollToCaret();
            txtOutput.Refresh();
        }
    }
}
